import numpy as np

''' numpy 计算 '''

a = np.array([10, 20, 30, 40])
b = np.arange(4)    # [0, 1, 2, 3]

# 简单计算
print(a, b)
print(b**2)
print(np.sin(a))
print(a>20)      # [False False  True  True]

print( a*b )
c = a.reshape(2,2)
d = b.reshape(2,2)
print(c, d)
print(np.dot(c, d))

# np.min() max, sum, mean, median

print(np.cumsum(d))  # 累加
print(np.diff(d)) # 累差
print(np.nonzero(d)) # 非0项

print(np.transpose(d))  # 转置  d.T

print(np.clip(d, 2, 3))  # 卡尔曼滤波 小于2换成2， 大于三换成3

a = np.array([1,2,4,5,5,6,8,3])
print(a.reshape(4,2))
print(a.reshape(4,2).flatten())

print(np.mean(a.reshape(4,2), axis=1)) # 0 列， 1 行

a = np.arange(1,10)
print(a.reshape(3,3))
print(np.mean(a.reshape(3,3), axis=1)) # 0 列， 1 行

print(a.reshape(3,3).flatten())
print(a.reshape(3,3).flat)


# 合并
A = np.array([1,1,1])
B = np.array([2,2,2])

print(np.vstack((A,B))) # 上下合并
print(np.hstack((A, B))) # 

print(A[:,np.newaxis])  # 秩
print(np.concatenate((A, B, A), axis=0))

# 分割
print(np.vsplit(np.arange(9).reshape(3,3), 3))
print(np.hsplit(np.arange(9).reshape(3,3), 3))

# 深拷贝
x = A.copy()
print(id(x), id(A))